<template>
  <div class="page-brand">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="brandName"
            :label="$t('brand.brandName')+':'"
            class="search-form-item"
          >
            <el-input
              v-model.trim="searchForm.brandName"
              clearable
              :placeholder="$t('brand.brandName')"
            />
          </el-form-item>
          <el-form-item
            prop="status"
            :label="$t('product.status')+':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.status"
              clearable
              :placeholder="$t('product.status')"
            >
              <el-option
                :label="$t('product.pendingReview')"
                :value="0"
              />
              <el-option
                :label="$t('productComm.pass')"
                :value="1"
              />
              <el-option
                :label="$t('shopProcess.reject')"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="searchChange(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetSearchForm"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con spec-table">
        <el-table
          ref="specListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            width="100"
            :label="$t('brand.serialNumber')"
          />

          <el-table-column
            prop="name"
            :label="$t('brand.brandName')"
            width="200"
          >
            <template #default="scope">
              <div class="table-cell-text">
                {{ scope.row.name }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="imgUrl"
            :label="$t('brandPopups.brandLogo')"
            width="150"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <ImgShow :src="scope.row.imgUrl" />
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="status"
            :label="$t('brand.auditRet')"
            width="100"
          >
            <template #default="scope">
              <span
                v-if="scope.row.auditStatus === 0"
                class="tag-text"
              >{{ $t("product.pendingReview") }}</span>
              <span
                v-else-if="scope.row.auditStatus === 1"
                class="tag-text"
              >{{ $t("productComm.pass") }}</span>
              <span
                v-else
                class="tag-text"
              >{{ $t("shopProcess.reject") }}</span>
            </template>
          </el-table-column>

          <!-- 平台分类 -->
          <el-table-column
            prop="platformClassification"
            :label="$t('product.platformClassification')"
            width="150"
            show-overflow-tooltip
          >
            <template #default="scope">
              <span>
                {{ scope.row.categories && scope.row.categories.length ? scope.row.categories.map(d => d.categoryName).join() : '--' }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            prop="qualifications"
            :label="$t('shopProcess.brandQualifications')"
            width="200"
          >
            <template #default="scope">
              <ImgsShow :src="scope.row.qualifications" />
            </template>
          </el-table-column>

          <el-table-column
            prop="createBy"
            :label="$t('brand.applyBy')"
            width="150"
          >
            <template #default="scope">
              <div class="table-cell-text">
                {{ scope.row.createdBy || '--' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="createTime"
            :label="$t('brand.applyTime')"
            width="200"
          >
            <template #default="scope">
              <div class="table-cell-text">
                {{ scope.row.createdTime || '--' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="auditBy"
            :label="$t('brand.auditBy')"
            width="150"
          >
            <template #default="scope">
              <div class="table-cell-text">
                {{ scope.row.updateBy || '--' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="auditTime"
            :label="$t('brand.auditTime')"
            width="200"
          >
            <template #default="scope">
              <div class="table-cell-text">
                {{ scope.row.updateTime || '--' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            fixed="right"
            :label="$t('publics.operating')"
            width="100"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="scope.row.auditStatus == 0"
                  class="default-btn text-btn"
                  @click="audit(scope.row)"
                >
                  {{ $t('shopProcess.audit') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 弹窗, 审核 -->
    <el-dialog
      v-model="visible"
      title="审核"
      :close-on-click-modal="false"
      width="570px"
    >
      <el-form
        ref="dataFormRef"
        :model="dataForm"
        :rules="dataRule"
        label-width="auto"
      >
        <!-- 审核结果 -->
        <el-form-item
          :label="$t('brand.auditRet')"
          prop="auditStatus"
        >
          <el-select
            v-model="dataForm.auditStatus"
            :placeholder="$t('tip.select')"
            style="width: 100%;"
            @change="changeStatus"
          >
            <el-option
              :label="$t('productComm.pass')"
              :value="1"
            />
            <el-option
              :label="$t('shopProcess.reject')"
              :value="2"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          :label="$t('crud.remark')"
          prop="remark"
        >
          <el-input
            v-model="dataForm.remark"
            type="textarea"
            maxlength="128"
            show-word-limit
            :rows="4"
            :placeholder="$t('crud.remark')"
            @blur="
              dataForm.remark =
                dataForm.remark ?
                  removeHeadAndTailSpaces(dataForm.remark) :
                  dataForm.remark
            "
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <div
            class="default-btn"
            @click="visible = false"
          >{{ $t("remindPop.cancel") }}</div>
          <div
            class="default-btn primary-btn"
            @click="dataFormSubmit()"
          >{{ $t("remindPop.confirm") }}</div>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { onMounted, reactive } from 'vue'
import { removeHeadAndTailSpaces } from '@/utils/validate.js'

const Data = reactive({
  dataForm: {
    auditStatus: '',
    remark: ''
  },
  dataRule: {
    auditStatus: [
      { required: true, message: $t('publics.noNull'), trigger: 'blur' }
    ],
    remark: [
      { required: false, message: $t('crud.remark') + $t('publics.noNull'), trigger: 'blur' }
    ]
  },
  dataList: [],
  dataListLoading: false,
  dataListSelections: [],
  addOrUpdateVisible: false,
  // 修改
  page: {
    total: 0, // 总页数
    currentPage: 1, // 当前页数
    pageSize: 10 // 每页显示多少条
  },
  searchForm: {
    brandName: '',
    status: ''
  }
})

const { dataForm, dataRule, dataList, page, searchForm } = toRefs(Data)

const visible = ref(false)

onMounted(() => {
  getDataList()
})

// 获取数据列表
const getDataList = (page, newData = false) => {
  if (Data.page) {
    const size = Math.ceil(Data.page.total / Data.page.pageSize)
    Data.page.currentPage = (Data.page.currentPage > size ? size : Data.page.currentPage) || 1
  }
  if (newData || !Data.theData) {
    Data.theData = JSON.parse(JSON.stringify(Data.searchForm))
  }
  Data.dataListLoading = true
  http({
    url: http.adornUrl('/brand/brandAudit/getBrandAuditRecordByPage'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: page == null ? Data.page.currentPage : page.currentPage,
          size: page == null ? Data.page.pageSize : page.pageSize
        },
        Data.theData
      )
    )
  }).then(({ data }) => {
    Data.dataList = data.records
    Data.page.total = data.total
    Data.dataListLoading = false

    // 末尾页数据为空重新请求
    if (!Data.dataList.length && data.current > 1) {
      Data.page.currentPage = 1
      getDataList()
    }
  })
}

const dataFormRef = ref()
// 新增 / 修改
const audit = (row) => {
  visible.value = true
  Data.dataForm.shopId = row.shopId
  Data.dataForm.brandId = row.brandId
  nextTick(() => {
    dataFormRef.value.resetFields()
  })
}

const changeStatus = (v) => {
  dataRule.value.remark[0].required = v == 2
}

// 表单提交
const dataFormSubmit = Debounce(() => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      const param = Data.dataForm
      http({
        url: http.adornUrl('/brand/brandAudit/auditBrand'),
        method: 'post',
        data: http.adornData(param)
      }).then(() => {
        ElMessage({
          message: $t('remindPop.success'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            refreshChange()
          }
        })
      })
    }
  })
}, 1500)

// 条件查询
const searchChange = (newData = false) => {
  Data.page.currentPage = 1
  getDataList(Data.page, newData)
}

// 刷新回调
const refreshChange = () => {
  getDataList(Data.page)
}

const searchFormRef = ref()
/**
* 重置表单
* @param {String} formName 表单名称
*/
const resetSearchForm = () => {
  searchFormRef.value.resetFields()
}

const handleSizeChange = (val) => {
  Data.page.pageSize = val
  getDataList()
}

const handleCurrentChange = (val) => {
  Data.page.currentPage = val
  getDataList()
}
</script>

<style lang="scss" scoped>

</style>
